<template>
  <div class="app-container">
    <!-- 查询 -->
    <tl-list-search ref="search" style="margin:6px 0" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" v-if="isHeaderDisplay">
      <template slot="search">
        <el-form-item class="qryItem" label="变更类型">
          <el-select v-model="queryParam.qry_sourcetype" clearable>
            <el-option v-for="item in codeModel.sourceTypeList" :key="item.code" :label="item.name" :value="item.code" clearable />
          </el-select>
        </el-form-item>
      </template>
    </tl-list-search>
    <!-- 工具栏 -->
    <tl-list-toolbar class="tl-list-toolbar" ref="toolbar" :refMainTable="$refs.mainTable" currentModuleTableCode="mpmProjectUniversalChange/main" :startColumn="3" @add="openAddDialog('editForm')" @exportExcel="doExportExcel" :isDisplayExport="false" @refresh="doRefresh" @columnCustomize="doColumnCustomize" @displaySearch="doDisplaySearch" :disableDeleteBatch="selectedRowKeys.length <= 0" :isDisplayAdd="false" :isDisplayDeleteBatch="false">
      <template slot="left-btn"> </template>
    </tl-list-toolbar>
    <div>
      <!-- 列表选择 -->
      <div class="table-alert-div" ref="selector">
        <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
          <template slot="title">
            已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRowKeys.length }}</a
            >项&nbsp;&nbsp;
            <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
          </template>
        </el-alert>
      </div>
      <!--表格渲染-->
      <el-table class="tl-table" ref="mainTable" :height="tableHeight" resizable-key="tableHeight" :resizable-refs="tableResizeRefs" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" @row-click="doRowClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column label="待处理" width="80px" align="center" header-align="center" :show-overflow-tooltip="true">
          <template v-slot="scope">
            <el-tag v-if="isInApprovalList(scope.row)" type="success">是</el-tag>
            <template v-else
              >否</template
            >
          </template>
        </el-table-column>
        <el-table-column prop="changecode" label="变更编号" width="150px" sortable="custom" sort-by="changecode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispsourcetype" label="来源类型" width="200px" sortable="custom" sort-by="sourcetype" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="changecontent" label="变更内容" width="250px" sortable="custom" sort-by="changecontent" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="changereason" label="变更原因" width="250px" sortable="custom" sort-by="changereason" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="influence" label="变更对项目的影响" width="250px" sortable="custom" sort-by="influence" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="submitdate" label="提交日期" width="170px" sortable="custom" sort-by="submitdate" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="latestphase" label="最新阶段" width="150px" sortable="custom" sort-by="latestphase" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="dispstate" label="状态" width="100px" sortable="custom" sort-by="state" align="center" header-align="center" :show-overflow-tooltip="true">
          <template v-slot="scope">
            <el-tag :type="changeStateType(scope.row.state)">
              {{ scope.row.dispstate }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="备注" min-width="250px" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100px" v-slot="scope" fixed="right">
          <el-dropdown trigger="hover">
            <el-button type="text" icon="el-icon-s-operation" style="margin-left:10px">更多<i class="el-icon-arrow-down el-icon--right"></i></el-button>
            <el-dropdown-menu>
              <el-dropdown-item icon="el-icon-view" @click.native="openViewDialogProxy(scope.row, 'id')">查看</el-dropdown-item>
              <el-dropdown-item icon="el-icon-view" :disabled="!scope.row.lastsourceid" @click.native="openLastVersionViewDialog(scope.row)">查看变更前记录</el-dropdown-item>
              <el-dropdown-item icon="el-icon-edit" :disabled="(scope.row.state !== 0 && scope.row.state !== 2) || scope.row.usercode !== user.code" @click="openEditDialogProxy(scope.row, 'id')">编辑</el-dropdown-item>
              <el-dropdown-item icon="el-icon-delete" :disabled="(scope.row.state !== 0 && scope.row.state !== 2) || scope.row.usercode !== user.code" @click="doDelete(scope.row.id)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-table-column>
      </el-table>
      <!-- 列表分页 -->
      <el-row class="pagebar" ref="pager">
        <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
      </el-row>
      <tl-edit ref="editForm" :projectinfo="projectinfo" @refresh="doRefresh"></tl-edit>
      <tl-change-edit ref="editChangeForm" :projectinfo="projectinfo" @refresh="doRefresh"></tl-change-edit>
      <tl-appointment-edit ref="editAppointmentForm" :projectinfo="projectinfo" @refresh="doRefresh"></tl-appointment-edit>
      <tl-plan-edit ref="editPlanForm" :projectinfo="projectinfo" @refresh="doRefresh"></tl-plan-edit>
      <tl-approval ref="approvalForm" @refresh="doRefresh"></tl-approval>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
/** 子组件 */
import tlEdit from './edit'
import tlChangeEdit from '../mpmProjectChange/edit'
import tlAppointmentEdit from '../mpmProjectAppointment/editDrawer'
import tlPlanEdit from '../mpmProjectPlan/edit'
import tlApproval from '../../cbo/cboApprovalNodeIns/approvalOne'
/** 工具类 */
import { checkPermission } from '@tapui/utils/permission'
import { getAction, putAction, postAction, deleteAction } from '@tapui/utils/http'
export default {
  name: 'mpmProjectUniversalChange',
  mixins: [mixin_list],
  components: { tlEdit, tlChangeEdit, tlAppointmentEdit, tlPlanEdit, tlApproval },
  data() {
    return {
      description: '项目变更通用-列表查询',
      urlPrefix: '/mpm/projectUniversalChange/',
      url: {
        queryCodeList: '/mpm/projectUniversalChange/dataCodeList',
        checkIsApproval: '/mpm/projectReview/checkIsApproval',
        queryApprovalList: '/cbo/approvalNodeIns/queryApprovalList'
      },
      reduceHeight: 264,
      // 排序参数
      sorterInfo: {
        sidx: 'sortcode',
        sord: 'asc'
      },
      queryParam: {
        qry_projectcode: this.projectinfo.projectcode
      },
      approvalDataList: [],
      sourcetype: 'MpmProjectUniversalChange'
    }
  },
  props: {
    projectinfo: {
      type: Object,
      default: function() {
        return {}
      }
    }
  },
  methods: {
    // 审批结束后刷新列表和顶部tab
    doRefresh() {
      this.loadData({})
      this.$emit('refreshIndex')
    },
    openApprovalDialog(id) {
      this.$refs.approvalForm.nodeInsId = id
      this.$refs.approvalForm.visible = true
    },
    /** 点击查看按钮 **/
    checkIsApproval(row) {
      if (row.state === 1) {
        var param = {
          sourceid: row.id,
          sourcetype: 'MpmProjectUniversalChange'
        }
        getAction(this.url.checkIsApproval, param).then((res) => {
          if (res.success) {
            if (res.result.isapprover) {
              this.openApprovalDialog(res.result.nodeinsid)
            } else {
              this.openNormalEdit(row)
            }
          } else {
            this.$tap.alert(res.message, 'warning')
          }
        })
      } else {
        this.openNormalEdit(row)
      }
    },
    openNormalEdit(row) {
      if (this.rowDbClickActionType === 'edit') {
        // 判断是否有权限
        if (this.editPermission.length > 0) {
          if (checkPermission(this.editPermission)) {
            this.openEditDialogProxy(row)
          } else {
            this.openViewDialogProxy(row)
          }
        } else {
          this.openEditDialogProxy(row)
        }
      } else if (this.rowDbClickActionType === 'view') {
        this.openViewDialogProxy(row)
      }
    },
    // 禁用表格前面的选择
    doDisableCheckBox(row, index) {
      if (row.state !== 0 && row.state !== 2) {
        return false
      } else {
        return true
      }
    },
    /** 根据选择的数据行来源类型打开不同的edit组件 **/
    openEditDialogProxy(rowData) {
      if (rowData.sourcetype === 'MpmProjectChange') {
        this.openViewDialog('editChangeForm', rowData.sourceid)
      } else if (rowData.sourcetype === 'MpmProjectAppointment') {
        this.openViewDialog('editAppointmentForm', rowData.sourceid)
      } else if (rowData.sourcetype === 'MpmProjectPlan') {
        this.openViewDialog('editPlanForm', rowData.sourceid)
      } else {
        if (rowData.usercode === this.user.code) {
          this.openEditDialog('editForm', rowData.id)
        } else {
          this.openViewDialog('editForm', rowData.id)
        }
      }
    },
    openViewDialogProxy(rowData) {
      if (rowData.sourcetype === 'MpmProjectChange') {
        this.openViewDialog('editChangeForm', rowData.sourceid)
      } else if (rowData.sourcetype === 'MpmProjectAppointment') {
        this.openViewDialog('editAppointmentForm', rowData.sourceid)
      } else if (rowData.sourcetype === 'MpmProjectPlan') {
        this.openViewDialog('editPlanForm', rowData.sourceid)
      } else {
        this.openViewDialog('editForm', rowData.id)
      }
    },
    /** 打开变更前的单据记录 **/
    openLastVersionViewDialog(rowData) {
      if (rowData.sourcetype === 'MpmProjectChange') {
        this.openViewDialog('editChangeForm', rowData.lastsourceid)
      } else if (rowData.sourcetype === 'MpmProjectAppointment') {
        this.openViewDialog('editAppointmentForm', rowData.lastsourceid)
      } else if (rowData.sourcetype === 'MpmProjectPlan') {
        this.openViewDialog('editPlanForm', rowData.lastsourceid)
      }
    },
    /** 表格双击事件重写 */
    doRowDbClick(row, column, event) {
      this.checkIsApproval(row)
    },
    beforeLoadData() {
      this.getApprovalDataList()
      return true
    },
    // 获取用户当前待审批列表 =>approvalDataList
    getApprovalDataList() {
      // 参数 在com.topscomm.mpm.cboController.CboApprovalNodeInsController#queryApprovalList 里面
      let approvalParam = {
        qry_sourcetype: this.sourcetype,
        qry_approvaluserid: this.user.id,
        sidx: 'nodeins.createon',
        currentPage: '1',
        pageSize: '1000'
      }
      getAction(this.url.queryApprovalList, approvalParam)
        .then((res) => {
          if (res.success) {
            this.approvalDataList = res.result.dataList
          }
        })
        .catch((err) => {
          this.$tap.alert(err, 'warninig')
        })
    },
    isInApprovalList(row) {
      return this.approvalDataList.filter((item) => item.sourceid === row.id).length > 0
    },
    /** 改变审核状态标签颜色 */
    changeStateType(state) {
      return state === 0 ? 'info' : state === 2 ? 'danger' : state === 9 ? 'success' : ''
    }
  }
}
</script>

<style scoped>
.app-container {
  margin: 0;
}
</style>
